/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visitante;

/**
 *
 * @author carlo
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import DBMS.DataBaseManager;
import clases.*;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class ConsultarMatriz extends DispatchAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String POPULATE = "populate";
    private static final String SOME = "some";

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ArrayList<Matriz> matrices = DataBaseManager.getInstance().consultarMatrices();
        request.setAttribute("matrices", matrices);
        return mapping.findForward(POPULATE);
    }


    public ActionForward oneUnidad(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager dbms = DataBaseManager.getInstance();
        Recordatorio r = (Recordatorio) form;
        Matriz m = new Matriz();
        m.setIdMatriz(Integer.parseInt(r.getMatriz()));
        Matriz matriz = dbms.consultarMatriz(m);
        System.out.println("matrizConsultada" + matriz.getNombre());
        for (Columna col : matriz.getColumnas()) {
            if ("Lista".equals(col.getTipo())) { 
                System.out.println("El tipo de la columna es lista" + col.getIdLista());
                ArrayList<Lista> listas = dbms.consultarListas();
                for (Lista lista : listas) {
                    if (lista.getIdLista() == (col.getIdLista())) {
                        col.setLista(lista);
                    }
                }
                System.out.println("\t  GUARDO LA LISTA " + col.getLista());
                request.setAttribute(col.getLista().getNombre(), col.getLista().getValores());
            }
        }
        // matriz.setNumeroColumnas(matriz.getColumnas().size());
        //  matriz.setNumeroFilas(matriz.getFilas().size());
        request.setAttribute("Matriz", matriz);
        request.setAttribute("recordatorio", r);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward some(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String codigoUnidad = request.getSession().getAttribute("categoria").toString();
        ArrayList<Matriz> matrices = DataBaseManager.getInstance().consultarMatricesPorUnidad(codigoUnidad);
        request.setAttribute("matrices", matrices);
        request.getSession().setAttribute("Ruta", new Ruta("Accion: Consulta Matriz"));
        return mapping.findForward(SOME);
    }


}


